import * as actionTypes from './actionTypes'
import axios from 'axios';


const changeHomeData = (data) => ({
  type: actionTypes.CHANGE_HOME_DATA,
  data
})

const addHomeList = (list, nextPage) => ({
	type: actionTypes.ADD_ARTICLE_LIST,
	list,
	nextPage
})


// 使用 redux-thunk 此时可以返回一个函数
export const getHomeInfo = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then((res) => {
      const resData = res.data
      if (resData.success) {
        dispatch(changeHomeData(resData.data))
      }
    }).catch((e) => {
      console.log(e);
    })
  }
}

export const getMoreList = (page) => {
  return (dispatch) => {
    axios.get('/api/homeList.json?page=' + page).then((res) => {
      const resData = res.data
      if (resData.success) {
        dispatch(addHomeList(resData.data, page + 1))
      }
    }).catch((e) => {
      console.log(e);
    })
  }
}

export const toggleTopShow = (show) => ({
	type: actionTypes.TOGGLE_SCROLL_TOP,
	show
})
